home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Info-Mac 4
/
Info_Mac IV CD-ROM (Pacific HiTech Inc.)(August 1994).iso
/
Development
/
Source
/
DBL Pascal Library
/
HashTables ƒ
/
HashTables_Test.p
< prev
next >
Wrap
Text File
|
1993-10-25
|
5KB
|
183 lines
program HashTables_Test;
uses
HashTables;
var
err: OSErr;
theTable: HashTable;
inFile: Text;
key: Str255;
value: Str255;
index: Integer;
replaced: Boolean;
entryCount: Longint;
slotCount: Integer;
efficiency: Real;
occupancy: Real;
state: Longint;
keyOffset: Longint;
keyLength: Integer;
valueOffset: Longint;
valueLength: Integer;
dataBlock: Handle;
recoverableSpace: Longint;
found: Boolean;
procedure MoveToString (data: Handle; offset: Longint; len: Integer; var result: Str255);
begin
BlockMove(Ptr(ORD(data^) + offset), Ptr(ORD(@result) + SIZEOF(SignedByte)), len);
{$PUSH}
{$R-}
result[0] := CHR(len);
{$POP}
end;
procedure Bzzt;
begin
WriteLn('Bzzt! State=', state : 1, ', key=', key, ', value=', value, '.');
end;
procedure Stats;
begin
err := HashTableSlotCount(theTable, slotCount);
WriteLn(slotCount : 5, ' slots.');
err := CountHashEntries(theTable, entryCount);
WriteLn(entryCount : 5, ' entries.');
err := HashRecoverableSpace(theTable, recoverableSpace);
WriteLn(recoverableSpace : 5, ' recoverable bytes.');
err := HashEfficiency(theTable, efficiency);
WriteLn(efficiency : 5 : 3, ' efficiency.');
err := HashSlotOccupancy(theTable, occupancy);
WriteLn(occupancy : 5 : 3, ' occupancy.');
end;
procedure Confirm;
begin
Reset(inFile);
index := 1;
repeat
ReadLn(inFile, key);
if key = '' then
Cycle;
value := '';
err := GetHashEntry(theTable, @key[1], length(key), valueOffset, valueLength, dataBlock, found);
if not found then
Bzzt;
index := index + 1;
until EOF(inFile);
end;
begin
{Setup.}
ShowText;
Open(inFile, OldFileName('Keys to read…'));
Reset(inFile);
{Create a test table.}
WriteLn('• Creating a new hash table.');
err := NewHashTable(kPrime67, 0, nil, nil, theTable);
{Load the table from a file.}
WriteLn('• Loading the table.');
index := 1;
repeat
ReadLn(inFile, key);
if key = '' then
Cycle;
value := StringOf(index : 1);
err := SetHashEntry(theTable, @key[1], length(key), @value[1], length(value), replaced);
index := index + 1;
until EOF(inFile);
{Make sure that all the entries exist.}
WriteLn('• Confirming.');
Confirm;
{Replace each entry with a longer value.}
WriteLn('• Replace values (longer)');
state := 0;
repeat
err := GetNextHashEntry(theTable, keyOffset, keyLength, valueOffset, valueLength, dataBlock, state);
if state <> 0 then
begin
MoveToString(dataBlock, keyOffset, keyLength, key);
MoveToString(dataBlock, valueOffset, valueLength, value);
value := concat(value, ' ', key);
err := SetHashEntry(theTable, @key[1], length(key), @value[1], length(value), replaced);
if not replaced then
Bzzt;
end;
until state = 0;
{Make sure that all the entries exist.}
WriteLn('• Confirming.');
Confirm;
{Replace each entry with a shorter value.}
WriteLn('• Replace values (shorter)');
state := 0;
repeat
err := GetNextHashEntry(theTable, keyOffset, keyLength, valueOffset, valueLength, dataBlock, state);
if state <> 0 then
begin
MoveToString(dataBlock, keyOffset, keyLength, key);
MoveToString(dataBlock, valueOffset, valueLength, value);
value := copy(value, pos(' ', value), MAXINT);
err := SetHashEntry(theTable, @key[1], length(key), @value[1], length(value), replaced);
if not replaced then
Bzzt;
end;
until state = 0;
{Make sure that all the entries exist.}
WriteLn('• Confirming.');
Confirm;
{Report some stats.}
WriteLn('• Initial stats.');
Stats;
{Rehash, report stats, and confirm entries a few times.}
WriteLn('• Rehashing.');
err := ReHash(theTable, kPrime139);
Stats;
WriteLn('• Confirming.');
Confirm;
{}
WriteLn('• Rehashing.');
err := ReHash(theTable, kPrime281);
Stats;
WriteLn('• Confirming.');
Confirm;
{}
WriteLn('• Rehashing.');
err := ReHash(theTable, kPrime563);
Stats;
WriteLn('• Confirming.');
Confirm;
{}
WriteLn('• Rehashing.');
err := ReHash(theTable, kPrime1129);
Stats;
WriteLn('• Confirming.');
Confirm;
{Delete half of the entries.}
WriteLn('• Delete half of entries.');
err := CountHashEntries(theTable, entryCount);
for index := 1 to entryCount div 2 do
begin
state := 0;
err := GetNextHashEntry(theTable, keyOffset, keyLength, valueOffset, valueLength, dataBlock, state);
err := RemoveHashEntry(theTable, Ptr(ORD(dataBlock^) + keyOffset), keyLength, found);
MoveToString(dataBlock, keyOffset, keyLength, key);
value := '';
if not found then
Bzzt;
end;
Stats;
{Compact the table.}
WriteLn('• Compacting.');
err := CompactHashSpace(theTable);
Stats;
{Empty the table.}
WriteLn('• Emptying.');
err := EmptyHashTable(theTable);
Stats;
{Dispose the table.}
WriteLn('• Disposing.');
err := DisposeHashTable(theTable);
{Done.}
Close(inFile);
end.